package com.wcar.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcar.common.entity.*;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.store.dao.CarDao;
import com.wcar.store.dao.CarStoreDao;
import com.wcar.store.dao.CarWashRecordDao;
import com.wcar.store.service.CarService;
import com.wcar.store.vo.BindPlusVo;
import com.wcar.store.vo.BindVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: four-wcar-idea
 * @BelongsPackage: com.wcar.store.service.impl
 * @Author: 李睢康
 * @CreateTime: 2024-06-04  16:31
 */
@Service
public class CarServiceImpl implements CarService {
    @Resource
    private CarDao carDao;
    @Resource
    private CarStoreDao carStoreDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private CarWashRecordDao carWashRecordDao;
    @Override
    public Result<Integer> insert(BindVo bindVo) {
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("font:login:" + token);
        FrontUser frontUser = JSON.parseObject(s, FrontUser.class);
        Car car = new Car();
        car.setUserId(bindVo.getUserId());
        car.setCarNo(bindVo.getCarNo());
        car.setCreateBy(frontUser.getUserName());
        car.setCreateTime(new Date());
        int insert = carDao.insert(car);
        //添加storecat
        CarStore carStore = new CarStore();
        carStore.setStoreId(bindVo.getStoreId());
        carStore.setCarId(car.getCarId());
        carStore.setCarNo(bindVo.getCarNo());
        int insert1 = carStoreDao.insert(carStore);
        return new Result<>(200,"添加车辆成功",insert1);
    }

    @Override
    public Result<List<CarStore>> selectByStoreId(BindPlusVo bindPlusVo) {
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("font:login:" + token);
        FrontUser frontUser = JSON.parseObject(s, FrontUser.class);
        QueryWrapper<Car> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",frontUser.getUserId());
        List<Car> cars = carDao.selectList(wrapper);
        System.out.println(cars+"!!!!!!!!!");
//        if(cars.equals("[]")){
//            return new Result<>(5000,"暂未添加车辆",null);
//        }
        QueryWrapper<CarStore> queryWrapper=new QueryWrapper<>();
        ArrayList<Integer> carIds=new ArrayList<>();
        for (Car c: cars) {
           carIds.add(c.getCarId());
        }
        queryWrapper.in("car_id",carIds);
        queryWrapper.eq("store_id",bindPlusVo.getStoreId());
        List<CarStore> carStores = carStoreDao.selectList(queryWrapper);
        return new Result<>(200,"查询店铺绑定车辆成功",carStores);
    }

    @Override
    public Result<List<CarWashRecord>> selectCar(BindPlusVo bindPlusVo) {
        QueryWrapper<CarWashRecord> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",bindPlusVo.getUserId());
        wrapper.eq("wash_status",0);
        List<CarWashRecord> carWashRecords = carWashRecordDao.selectList(wrapper);
        return new Result<>(200,"查询车辆成功",carWashRecords);
    }

}
